package com.luxoft.scoremanager.server.dao.hibernateimpl;

import com.luxoft.scoremanager.server.dao.ComponentValuesDao;
import com.luxoft.scoremanager.server.dao.exception.HibernateRecordException;
import com.luxoft.scoremanager.server.dao.hibernateimpl.util.HibernateUtil;
import com.luxoft.scoremanager.server.model.dao.ComponentValuesDaoModel;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class HibernateComponentValuesDao implements ComponentValuesDao {
    @Autowired
    private static Logger log = Logger.getLogger(HibernateComponentValuesDao.class);

    @Override
    public void addComponentValues(ComponentValuesDaoModel component) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(component);
            session.getTransaction().commit();
        } catch (Exception e) {

            log.info("Error component value add");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updateComponentValues(ComponentValuesDaoModel component) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(component);
            session.getTransaction().commit();
        } catch (Exception e) {

            log.info("Error component value update");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public void deleteComponentValues(ComponentValuesDaoModel component) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(component);
            session.getTransaction().commit();
        } catch (Exception e) {

            log.info("Error component value delete");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public List<ComponentValuesDaoModel> getAll() {
        Session session = null;
        List componentValueList = new ArrayList<ComponentValuesDaoModel>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            componentValueList = session.createCriteria(ComponentValuesDaoModel.class).list();
        } catch (Exception e) {

            log.info("Error component value get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return componentValueList;
    }

    @Override
    public List<ComponentValuesDaoModel> getAllForPackage(int id) {
        Session session = null;
        List componentValueList = new ArrayList<ComponentValuesDaoModel>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            String hql = "FROM ComponentValuesDaoModel E WHERE E.scorePackageDaoModel.id = :package_id";
            Query query = session.createQuery(hql);
            query.setParameter("package_id", id);
            componentValueList = query.list();
        } catch (Exception e) {

            log.info("Error component value get all for package");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return componentValueList;
    }

    @Override
    public ComponentValuesDaoModel getById(int id) {
        Session session = null;
        ComponentValuesDaoModel componentValues = new ComponentValuesDaoModel();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            componentValues = (ComponentValuesDaoModel) session.get(ComponentValuesDaoModel.class, id);
        } catch (Exception e) {

            log.info("Error component value get by id");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return componentValues;
    }
}
